﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ApplicationBlocks.Data;
using NoidungComponent.BLL;
using System.Data;
using System.Data.SqlClient;

namespace NoidungComponent.DAL
{
    public class NoidungDAL:INoidungDAL
    {
        #region INoidung Connection
        private string _connectionstring=Commom.WCManager.ConnectionString();

        public string Connection
        {
            get { return _connectionstring; }
            set { _connectionstring = value; }
        }

        #endregion

        #region INoidung Member
        #region Get
        public void Get(Noidung noidung)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(this.Connection))
                {
                    SqlParameter[] par = new SqlParameter[1];
                    par[0] = new SqlParameter("@noi_dung_id", noidung.Noi_dung_id);
                    using (SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "P_noi_dung_Get", par))
                    {
                        while (dr.Read())
                        {
                            noidung.Noi_dung_id = int.Parse(dr["noi_dung_id"].ToString());
                            noidung.Chu_de_id = int.Parse(dr["chu_de_id"].ToString());
                            noidung.Ma = dr["ma"].ToString();
                            noidung.Tieude = dr["tieude"].ToString();
                            noidung.Anh = dr["anh"].ToString();
                            noidung.Mieuta = dr["mieuta"].ToString();
                            noidung.Noi_dung = dr["noidung"].ToString();
                            noidung.Url = dr["url"].ToString();
                            noidung.Ngaytao = dr["ngaytao"].ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            { throw; }
        }
        #endregion

        #region Add
        public int Add(Noidung noidung)
        {
            try
            {
                int RowNo = -1;
                using (SqlConnection conn = new SqlConnection(this.Connection))
                {
                    SqlParameter[] par = new SqlParameter[9];
                    par[0] = new SqlParameter("@noi_dung_id", noidung.Noi_dung_id);
                    par[0].Direction = ParameterDirection.Output;
                    par[1] = new SqlParameter("@chu_de_id", noidung.Chu_de_id);
                    par[2] = new SqlParameter("@ma", noidung.Ma);
                    par[3] = new SqlParameter("@tieude", noidung.Tieude);
                    par[4] = new SqlParameter("@anh", noidung.Anh);
                    par[5] = new SqlParameter("@mieuta", noidung.Mieuta);
                    par[6] = new SqlParameter("@noidung", noidung.Noi_dung);
                    par[7] = new SqlParameter("@url", noidung.Url);
                    par[8] = new SqlParameter("@ngaytao", noidung.Ngaytao);
                    SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "P_noi_dung_Insert", par);
                    RowNo = int.Parse(par[0].Value.ToString());
                }
                return RowNo;
            }
            catch
            { throw; }
        }
        #endregion

        #region Update
        public void Update(Noidung noidung)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(this.Connection))
                {
                    SqlParameter[] par = new SqlParameter[9];
                    par[0] = new SqlParameter("@noi_dung_id", noidung.Noi_dung_id);
                    par[1] = new SqlParameter("@chu_de_id", noidung.Chu_de_id);
                    par[2] = new SqlParameter("@ma", noidung.Ma);
                    par[3] = new SqlParameter("@tieude", noidung.Tieude);
                    par[4] = new SqlParameter("@anh", noidung.Anh);
                    par[5] = new SqlParameter("@mieuta", noidung.Mieuta);
                    par[6] = new SqlParameter("@noidung", noidung.Noi_dung);
                    par[7] = new SqlParameter("@url", noidung.Url);
                    par[8] = new SqlParameter("@ngaytao", noidung.Ngaytao);
                    SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "P_noi_dung_Update", par);
                }
            }
            catch (Exception ex)
            { throw; }
        }
        #endregion

        #region Delete
        public void Delete(Noidung noidung)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(this.Connection))
                {
                    SqlParameter[] par = new SqlParameter[1];
                    par[0] = new SqlParameter("@noi_dung_id", noidung.Noi_dung_id);
                    SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "P_noi_dung_Delete", par);
                }
            }
            catch { throw; }
        }
        #endregion

        #region GetAll
        public List<Noidung> GetAll(int currentPage, int pageSize, out int total, string tieude, string column_sort, string type_sort)
        {
            try
            {
                List<Noidung> soList = new List<Noidung>();
                using (SqlConnection con = new SqlConnection(this.Connection))
                {
                    SqlParameter[] par = new SqlParameter[6];
                    par[0] = new SqlParameter("@currentpage", currentPage);
                    par[1] = new SqlParameter("@pagesize", pageSize);
                    par[2] = new SqlParameter("@total", SqlDbType.Int);
                    par[2].Direction = ParameterDirection.Output;
                    par[3] = new SqlParameter("@solution_name", tieude);
                    par[4] = new SqlParameter("@column_sort", column_sort);
                    par[5] = new SqlParameter("@type_sort", type_sort);

                    using (SqlDataReader dr = SqlHelper.ExecuteReader(con, CommandType.StoredProcedure, "P_tai_nguyen_GetAll", par))
                    {
                        while (dr.Read())
                        {
                            Noidung noidung = new Noidung();
                            noidung.Noi_dung_id = int.Parse(dr["noi_dung_id"].ToString());
                            noidung.Chu_de_id = int.Parse(dr["chu_de_id"].ToString());
                            noidung.Ma = dr["ma"].ToString();
                            noidung.Tieude = dr["tieude"].ToString();
                            noidung.Anh = dr["anh"].ToString();
                            noidung.Mieuta = dr["mieuta"].ToString();
                            noidung.Noi_dung = dr["noidung"].ToString();
                            noidung.Url = dr["url"].ToString();
                            noidung.Ngaytao = dr["ngaytao"].ToString();
                        }
                    }
                    total = int.Parse(par[2].Value.ToString());
                }
                return soList;
            }
            catch (Exception ex) { throw; }
        }
        #endregion

        #endregion
    }
}
